package com.hou.serviceImpl;

import com.hou.EX.ServicesException.IdnotException;
import com.hou.mapper.TaomiGoodsMapper;
import com.hou.vo.TaomiGoods;
import com.hou.service.TaomiGoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.system.ApplicationHome;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

@Service
public class TaomiGoodsServiceImpl implements TaomiGoodsService {

    @Autowired
    private TaomiGoodsMapper taomiGoodsMapper;

    @Override
    public void addTaomiGoods(TaomiGoods taomiGoods) {
        Integer rows = taomiGoodsMapper.add(taomiGoods);

        if (rows != 1) {
            throw new IdnotException("添加失败，可能你的id值已经存在");
        }
    }

    @Override
    public void updateTaomiGoods(TaomiGoods taomiGoods) {
        Integer rows = taomiGoodsMapper.update(taomiGoods);
        if (rows != 1) {
            throw new IdnotException("修改失败，可能是你的id值没有找到，请确定你的id值是否存在");
        }
    }

    @Override
    public List<TaomiGoods> selectTaomiGoods(String name) {
        List<TaomiGoods> select;
        try {
            select = taomiGoodsMapper.select(name);
        }catch(Exception ex){
            return null;
        }
        return select;
    }

    @Override
    public TaomiGoods selectidTaomiGoods(Long id) {
        TaomiGoods taomiGoods = taomiGoodsMapper.selectid(id);

        return taomiGoods;
    }


    @Override
    public void deleteTaomiGoods(Long id) {
        Integer rows = taomiGoodsMapper.delete(id);
        if (rows != 1) {
            throw new IdnotException("删除失败，可能是你的id值没有找到，请确定你的id值是否存在");
        }
    }

    @Override
    public List<TaomiGoods> selectTaomiGoodsList() {
        List<TaomiGoods> select;
        try {
            select = taomiGoodsMapper.selectLists();
        }catch(Exception ex){
            return null;
        }
        return select;
    }

//    @Override
//    public String changeAvatar(MultipartFile file, HttpServletRequest request) throws IOException {
//        //通过servletContext获取服务器中photo目录的路径
////        String parent = session.getServletContext().getRealPath("/classes/static/images/photo/");
//        String parent = request.getServletContext().getRealPath("upload");
//        //获取jar包所在目录
////        ApplicationHome h = new ApplicationHome(getClass());
////        File jarF = h.getSource();
////        //在jar包所在目录下生成一个upload文件夹用来存储上传的图片
////        String parent = jarF.getParentFile().toString()+"/classes/static/images/book/";
//        // 保存头像文件的文件夹
//        File dir = new File(parent);
//        if (!dir.exists()) {
//            dir.mkdirs();
//        }
//
//        // 保存的头像文件的文件名
//        String originalFilename = file.getOriginalFilename();
//
//        String suffix = originalFilename.substring(originalFilename.lastIndexOf("."));
//        String filename = UUID.randomUUID().toString() + suffix;
//
//        // 创建文件对象，表示保存的头像文件
//        File dest = new File(dir, filename);
//
////        //获取jar包所在目录
////        ApplicationHome h = new ApplicationHome(getClass());
////        File jarF = h.getSource();
////        //在jar包所在目录下生成一个upload文件夹用来存储上传的图片
////        String parent = jarF.getParentFile().toString()+"/classes/static/images/book/";
//////        System.out.println(parent);
////
////        // 保存头像文件的文件夹
////        File dir = new File(parent);
////        if (!dir.exists()) {
////            dir.mkdirs();
////        }
////
////        // 保存的头像文件的文件名
////        String suffix = "";
////        String originalFilename = file.getOriginalFilename();
////        int beginIndex = originalFilename.lastIndexOf(".");
////        if (beginIndex > 0) {
////            suffix = originalFilename.substring(beginIndex);
////        }
////        NewBooks newBooks = newsBootService.changePicUrl(title);
////        String string = newBooks.getPicurl();
////        String[] split = string.split("\\.");
////        String filename =split[0]+ suffix;
////
////        // 创建文件对象，表示保存的头像文件
////        File dest = new File(dir, filename);
////        // 执行保存头像文件
////        file.transferTo(dest);
//
//        // 执行保存头像文件
//        file.transferTo(dest);
//
//        // 头像路径
//        String avatar = "/upload/" + filename;
//        // 头像路径
////        String avatar = "/images/book/" + filename;
//
//        return avatar;
//    }



}
